package geektime.spring.springbuks.service.db1.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import geektime.spring.springbuks.bean.dp1.Account;
import geektime.spring.springbuks.mapper.db1.AccountMapper;
import geektime.spring.springbuks.service.db1.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public PageInfo<Account> selectAccount(Long userId, int page, int pageSize) {

        PageHelper.startPage(pageSize, page);
        List<Account> accounts=accountMapper.selectByPrimaryAll(userId,page,pageSize);

        return new PageInfo<>(accounts);
    }

    @Override
    public int insertAccount(Account account) {
        return accountMapper.insert(account);
    }

    @Override
    public int UpdateAccount(Account account) {
        //查詢記錄
        Account account1=accountMapper.selectByPrimaryKey(account.getId());
        if (account1==null){
            return 0;
        }
        Account account2=new Account();
        account2.setPassword(account.getPassword());
        account2.setSalt(account.getSalt());
        int updateStatus=accountMapper.updateByPrimaryKey(account2);
        return updateStatus;

    }

    @Override
    public int delectAccount(Long id) {
        //查詢記錄
        Account account1=accountMapper.selectByPrimaryKey(id);
        if (account1==null) {
            return 0;
        }
        int delectStatus=accountMapper.delectAccount(id);
        return delectStatus;
    }
}
